/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;
import Entity.InfoDatabase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.jdbc.odbc.JdbcOdbcDriver;
/**
 *
 * @author dung
 */
public class DBUtil {
    public static Connection conn = null;
    private static String fileconfig = "config.properties";
    public static InfoDatabase SetInfoDB(InfoDatabase idb){
        Properties p = new Properties();
        try{
            FileOutputStream file = new FileOutputStream(fileconfig);
            p.setProperty("server", idb.getServer());
            p.setProperty("port", idb.getPort());
            p.setProperty("Database", idb.getDatabase());
            p.setProperty("user", idb.getUser());
            p.setProperty("pass", idb.getPass());
            p.store(file, "SALES and INVENTORY MANAGEMENT - C0909L - FPT - Aptech 2");
            file.close();
        }catch(IOException ex)
        {
            System.out.println("Error :" + ex);
        }
        return idb;
    }
    public static InfoDatabase getInfoDB(){
        Properties p = new Properties();
        InfoDatabase idb = null;
        File file = new File(fileconfig);
        if(file!=null||file.exists())
        {
            try {
                FileInputStream fis = new FileInputStream(file);
                p.load(fis);
                idb = new InfoDatabase(p.getProperty("server"),
                        p.getProperty("port"), p.getProperty("Database"),
                        p.getProperty("user"), p.getProperty("pass"));
            } catch(Exception ex) {
                Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return idb;
    }
    public static Connection getConnection(InfoDatabase idb)
    {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn=DriverManager.getConnection("jdbc:sqlserver://"
                    +idb.getServer()+":"+idb.getPort()+";DatabaseName = "
                    +idb.getDatabase()+";User = "+idb.getUser()+";Password = "+idb.getPass());
        } catch (SQLException ex) {
            Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch(Exception ex)
        {
            return  null;
        }
        return conn;
    }
    public static void close(Connection conn)
    {
        try {
            if (conn != null || conn.isClosed()) {
                conn.close();
                conn = null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
